capture log close
log using "${path_log}/03_read_bop_f.log", replace

*** Create data set with the sector, region, and type of respondent information for each wave	
clear all 
	  
qui forval w = 1/17 {
	
	if !inlist(`w',1,2,6,7,14,15,16,17)  append using "${path_orig}\BOPF\bop_f_wave`w'_v1.dta"
	else if inlist(`w',1) 	 		  	 append using "${path_orig}\BOPF\bop_f_wave0`w'_v1.dta"
	else 			   			   	  	 append using "${path_orig}\BOPF\bop_f_wave`w'_v2.dta"
					
	preserve 
	
	ren (respondenttype_position_f) (res_position_f)
	
	*** Handle missings
	recode res_position_f firmsector firmregion (-6666 -9997 -9998 -9999 = .)
	
	*** Search latest wave with non-missing answer 
	bysort BBk_Sample_Key (wave): g counter = _n
	
	foreach v of varlist firmsector firmregion res_position_f {
		
		gegen counter_`v' = max(counter) if !mi(`v'), by(BBk_Sample_Key)
		bysort BBk_Sample_Key: g aux_`v'_l = `v' if counter_`v' == counter 
		gegen `v'_l = max(aux_`v'_l), by(BBk_Sample_Key)
	
	}
	
	*** Keep only the latest wave
	keep if wave == `w'
			
	tempfile firmtype_`w'
	
	save `firmtype_`w''
	
	restore 
	 
} // y 

clear all

qui forval w = 1/17 {

	append using `firmtype_`w''

} // y 

keep BBk_Sample_Key wave res_position_f_l firmsector_l firmregion_l
		
tempfile firmtype
	
save `firmtype'

*** Append all waves
clear all

qui forval w = 1/17 {
	
	if !inlist(`w',1,2,6,7,14,15,16,17)  append using "${path_orig}\BOPF\bop_f_wave`w'_v1.dta"
	else if inlist(`w',1) 	 		  	 append using "${path_orig}\BOPF\bop_f_wave0`w'_v1.dta"
	else 			   			   	  	 append using "${path_orig}\BOPF\bop_f_wave`w'_v2.dta"
	
} // w

*** Merge with firmtype
merge 1:1 BBk_Sample_Key wave using `firmtype'
keep if _merge == 3
drop _merge 

destring BBk_Sample_Key, replace

*** Generate dummy for no change
replace energy_price_change_time = 0 if energy_price_change_time == -6666 // Does not apply = no change
g no_change = energy_price_change_time == 0 if !mi(energy_price_change_time)

*** Recode missings
qui recode energy_prod energy_price_change_? energy_price_change_time energymix_? inflexpoint firmemploy pricefuture emplpast_? firmemploy_? percprob_? pricepast interr salespast (-6666 -9999 -9998 -9997 -9996 = .)

*** Extend variables to all waves
qui foreach v of varlist energy_price_change_time energymix_? energy_price_change_? energy_cons_change energy_prod no_change {
	
	gegen aux_`v' = max(`v'), by(BBk_Sample_Key)
	replace `v' = aux_`v'
	drop aux_`v'

} // v

xtset BBk_Sample_Key wave
tsfill, full

*** Shift sales one quarter back since it is elicited for the past quarter 
g sales = F3.salespast if wave > 5

*** Employment
g empl2021q3 = emplpast_b if inrange(wave,9,11) // employment in 2021Q3
g empl2021q4 = firmemploy if inrange(wave,12,17) // employment in 2021Q4
g empl2022q1 = emplpast_b if inrange(wave,15,17) // employment in 2022Q1

foreach v of varlist empl202* {

	ereplace `v' = max(`v'), by(BBk_Sample_Key)

} // v

*** Assign employment numbers to their respective dates 
g empl = empl2021q3 if year == 21 & inrange(month,7,9)
replace empl = empl2021q4 if year == 21 & inrange(month,10,12) 
replace empl = empl2022q1 if year == 22 & inrange(month,1,3) 

*** Normalize sales to € millions
replace sales = sales / 1000

*** Drop if no answer to price change
keep if energy_price_change_time != .

g inflexpoint_orig = inflexpoint 

*** Account for outlier
winsor2 inflexpoint pricefuture pricepast, cuts(2 98) trim replace 
 
*** Merge with inflation data
merge m:1 wave using "${path_data}\cpi_f.dta"
keep if _merge != 2
drop _merge 

*** Merge with professional forecast data
merge m:1 wave using "${path_data}\forecast_f.dta"
keep if _merge != 2
drop _merge 
 
*** Prediction errors 
g abs_predict_error = abs(inflexpoint - cpi_f12m)
g sq_predict_error = (inflexpoint - cpi_f12m)^2
g abs_difference_spf = abs(inflexpoint - consensus_forecast_int)
g sq_difference_spf = (inflexpoint - consensus_forecast_int)^2
 
gsort BBk_Sample_Key wave

*** Shift price changes one quarter since they refer to the last quarter
g price_current = F3.pricepast 

*** Price change data does not cover 2022 Q2
keep if wave < 15

*** Appendix Figure A.3b
preserve
	
collapse (mean) inflexpoint, by(wave)

drop if mi(inflexpoint)

twoway connected inflexpoint wave, graphregion(color(white)) bgcolor(white) plotregion(fcolor(white)) xlabel(6 "Jul 2021" 7 "Aug 2021" 8 "Sep 2021" 9 "Oct 2021" 10 "Nov 2021" 11 "Dec 2021" 12 "Jan 2022" 13 "Feb 2022" 14 "Mar 2022", angle(90)) xtitle("") ytitle("1-year inflation expectation (in pp)") lcolor(blue) mcolor(blue)

graph export "${path_export_graphs}\appendix_figure_a3b.pdf", replace

restore 

*** Quantative Price Change 2021
g energy_price_change_2021 = -0.20 		  if energy_price_change_a == 1
replace energy_price_change_2021 = -0.15  if energy_price_change_a == 2
replace energy_price_change_2021 = -0.075 if energy_price_change_a == 3
replace energy_price_change_2021 =  0 	  if energy_price_change_a == 4
replace energy_price_change_2021 =  0.075 if energy_price_change_a == 5
replace energy_price_change_2021 =  0.15  if energy_price_change_a == 6
replace energy_price_change_2021 =  0.25  if energy_price_change_a == 7
replace energy_price_change_2021 =  0.35  if energy_price_change_a == 8
replace energy_price_change_2021 =  0.45  if energy_price_change_a == 9
replace energy_price_change_2021 =  0.55  if energy_price_change_a == 10
replace energy_price_change_2021 =  0.65  if energy_price_change_a == 11
replace energy_price_change_2021 =  0.70  if energy_price_change_a == 12

*** Alternative assumptions about end points: +- 5
g energy_price_change_2021_5 = energy_price_change_2021
replace energy_price_change_2021_5 = - 0.25 if energy_price_change_a == 1
replace energy_price_change_2021_5 =   0.75 if energy_price_change_a == 12

*** Alternative assumptions about end points: +- 10
g energy_price_change_2021_10 = energy_price_change_2021
replace energy_price_change_2021_10 = - 0.30 if energy_price_change_a == 1
replace energy_price_change_2021_10 =   0.80 if energy_price_change_a == 12

*** Quantative Price Change 2022
g energy_price_change_2022 = -0.20 		  if energy_price_change_b == 1
replace energy_price_change_2022 = -0.15  if energy_price_change_b == 2
replace energy_price_change_2022 = -0.075 if energy_price_change_b == 3
replace energy_price_change_2022 =  0 	  if energy_price_change_b == 4
replace energy_price_change_2022 =  0.075 if energy_price_change_b == 5
replace energy_price_change_2022 =  0.15  if energy_price_change_b == 6
replace energy_price_change_2022 =  0.25  if energy_price_change_b == 7
replace energy_price_change_2022 =  0.35  if energy_price_change_b == 8
replace energy_price_change_2022 =  0.45  if energy_price_change_b == 9
replace energy_price_change_2022 =  0.55  if energy_price_change_b == 10
replace energy_price_change_2022 =  0.65  if energy_price_change_b == 11
replace energy_price_change_2022 =  0.70  if energy_price_change_b == 12

*** Alternative assumptions about end points: +- 5
g energy_price_change_2022_5 = energy_price_change_2022
replace energy_price_change_2022_5 = - 0.25 if energy_price_change_b == 1
replace energy_price_change_2022_5 =   0.75 if energy_price_change_b == 12

*** Alternative assumptions about end points: +- 10
g energy_price_change_2022_10 = energy_price_change_2022
replace energy_price_change_2022_10 = - 0.30 if energy_price_change_b == 1
replace energy_price_change_2022_10 =   0.80 if energy_price_change_b == 12

*** Overall price change 
g energypricechange = (1 + energy_price_change_2021) * (1 + energy_price_change_2022) - 1 if !mi(energy_price_change_2021,energy_price_change_2022)
replace energypricechange = energypricechange * 100

*** Overall price change: alternative assumptions about end points: +- 5 
g energypricechange5 = (1 + energy_price_change_2021_5) * (1 + energy_price_change_2022_5) - 1 if !mi(energy_price_change_2021_5,energy_price_change_2022_5)
replace energypricechange5 = energypricechange5 * 100

*** Overall price change: alternative assumptions about end points: +- 10  
g energypricechange10 = (1 + energy_price_change_2021_10) * (1 + energy_price_change_2022_10) - 1 if !mi(energy_price_change_2021_10,energy_price_change_2022_10)
replace energypricechange10 = energypricechange10 * 100

*** Change treatment timing to 2022 Q1 if no price change in 2021 and positive change in 2022 Q1
replace energy_price_change_time = 5 if inrange(energy_price_change_time,1,4) & energy_price_change_2021 == 0 & energy_price_change_2022 > 0 & !mi(energy_price_change_2022)
replace energypricechange = . if no_change == 1

*** Dummy for price decrease
g price_decrease = energy_price_change_2021 < 0 | energy_price_change_2022 < 0 if !mi(energy_price_change_2021,energy_price_change_2022)

*** Quarter 
g quarter = 1 if year == 21 & inlist(month,1,2,3)
replace quarter = 2 if year == 21 & inlist(month,4,5,6)
replace quarter = 3 if year == 21 & inlist(month,7,8,9)
replace quarter = 4 if year == 21 & inlist(month,10,11,12)
replace quarter = 5 if year == 22 & inlist(month,1,2,3)

*** Generate treatment dummies
g price_change = energy_price_change_time == 1 & quarter == 1 if !mi(energy_price_change_time)
replace price_change = 1 if energy_price_change_time == 2 & quarter == 2
replace price_change = 1 if energy_price_change_time == 3 & quarter == 3
replace price_change = 1 if energy_price_change_time == 4 & quarter == 4
replace price_change = 1 if energy_price_change_time == 5 & quarter == 5

bysort BBk_Sample_Key (wave): g tau = sum(price_change)

g month_price_change = wave if price_change == 1
replace month_price_change = 0 if no_change == 1

*** Scaled Treatment Indicator
g tau_energypricechange = tau * energypricechange 

*** Dummy for interruption
recode interr (2=1) (1=0)
 
*** Small firms  
g aux_d_smallfirm = empl <= 50 if !mi(empl) & wave >= 6 // Define firm size before energy price increased
gegen min_wave = min(wave) if !mi(aux_d_smallfirm), by(BBk_Sample_Key) // Find wave with first info
replace aux_d_smallfirm = . if wave != min_wave
gegen d_smallfirm = max(aux_d_smallfirm), by(BBk_Sample_Key) // Always take first info to avoid endogneity 
 
*** Energy cost share
g share_cost_energy = 0 if energy_prod == 1
replace share_cost_energy = 0.05 if energy_prod == 2
replace share_cost_energy = 0.15 if energy_prod == 3
replace share_cost_energy = 0.25 if energy_prod == 4
replace share_cost_energy = 0.35 if energy_prod == 5
replace share_cost_energy = 0.45 if energy_prod == 6
replace share_cost_energy = 0.55 if energy_prod == 7
replace share_cost_energy = 0.65 if energy_prod == 8
replace share_cost_energy = 0.75 if energy_prod == 9
replace share_cost_energy = 0.85 if energy_prod == 10
replace share_cost_energy = 0.95 if energy_prod == 11
replace share_cost_energy = 1.00 if energy_prod == 12

*** Respondent type
g d_decisionmaker = res_position_f_l == 1 if !mi(res_position_f_l)

*** Energy intensive sectors
g d_sector_intensive = inlist(firmsector_l,2,4,5,6,7) // Mining + manufacturing

replace energymix_f = energymix_f + energymix_g // Renewable + convential electricity
replace energymix_h = energymix_h + energymix_b + energymix_c // other energy sources
drop energymix_b energymix_c energymix_g

*** Dummies for electricity expenditure share
qui sum energymix_f, det 
g d_elec_50 = energymix_f >= 50 if !mi(energymix_f)
g d_elec_75 = energymix_f >= 75 if !mi(energymix_f)
g d_elec_90 = energymix_f >= 90 if !mi(energymix_f)

*** Dummies for number of energy sources
g d_sources_one = (energymix_a == 0 & energymix_e == 0) | (energymix_a == 0 & energymix_f == 0) | (energymix_e == 0 & energymix_f == 0) if !mi(energymix_a,energymix_e,energymix_f)
g d_sources_two = ((energymix_a == 0) | (energymix_e == 0) | (energymix_f == 0)) & d_sources_one == 0  if !mi(energymix_a,energymix_e,energymix_f)
g d_sources_three = d_sources_one == 0 & d_sources_two == 0 if !mi(energymix_a,energymix_e,energymix_f)

qui foreach v of varlist energymix_? {
	
	replace `v' = `v' / 100
	
} // v

*** Assign treatment quarter
g aux_treatment_time = wave if energy_price_change_time == quarter
gegen treatment_time = max(aux_treatment_time), by(BBk_Sample_Key)
drop aux_treatment_time

*** Drop public sector firms
drop if firmsector_l == 20 

*** Sectoral dummies
g d_sector_manuf = inlist(firmsector_l,4,5,6,7) if !mi(firmsector)
g d_sector_const = firmsector_l == 8 if !mi(firmsector)
g d_sector_commerce = inlist(firmsector_l,9,10) if !mi(firmsector)
g d_sector_otherservices = inrange(firmsector_l,11,19) if !mi(firmsector)
g d_sector_other = inlist(firmsector_l,1,2,3) if !mi(firmsector)

*** Generate Huber weights
qui rreg inflexpoint_orig tau if price_decrease != 1, genwt(wgt_outlier)
 
*** Save data set including pilot phase as problem perception was asked throughout
preserve 

*** Drop firm that reported a price decrease
drop if price_decrease == 1
bysort BBk_Sample_Key: g num_wave = _N
drop if num_wave == 1 // Keep only panel firms

*** Appendix Table A.5: Part one
qui estpost sum percprob_*, det  
est store a

esttab a using "${path_export_tables}\appendix_table_a5b_part1.tex", replace nomtitles collabels(\multicolumn{1}{c}{mean} \multicolumn{1}{c}{median} \multicolumn{1}{c}{sd} \multicolumn{1}{c}{N}) cells("mean(fmt(2)) p50(fmt(2)) sd(fmt(2)) count(fmt(0))") coeflabels(percprob_a "problems with customer demand" percprob_b "problems with competitive pressure" percprob_c "problems with credit supply" percprob_d "problems with supply chains" percprob_e "problems with production costs" percprob_f "problems with availability of personnel" percprob_g "problems with regulations" percprob_h "problems with Covid-19 restrictions") nonumber f noobs alignment(S) booktabs postfoot("\bottomrule \bottomrule")

est clear 
 
*** Normalize by standard deviation
qui foreach v of varlist percprob_? { 
	
	qui sum `v' 
	replace `v' = `v' / `r(sd)'  
	
}

keep BBk_Sample_Key wave treatment_time percprob_*

save "${path_data}\bopf_early.dta", replace

restore 

keep if wave >= 6 // Drop pilot phase data
bysort BBk_Sample_Key: g num_wave = _N
drop if num_wave == 1 // Keep only panel firms

*** Save data set with firm that reported a price decrease
preserve 

keep BBk_Sample_Key wave treatment_time inflexpoint treatment_time tau interr d_decisionmaker price_decrease energypricechange

bysort BBk_Sample_Key (wave): g firm_time = _n
xtset BBk_Sample_Key firm_time 

compress 

save "${path_data}\bopf_wdec.dta", replace

restore 

*** Drop firm that reported a price decrease
drop if price_decrease == 1 

*** Firm size fixed effects 
gegen empl_mean = mean(empl), by(BBk_Sample_Key)
xtile empl_q = empl_mean, nq(4)
gegen empl_q_wave = group(empl_q wave)

*** Dummy gas-intensive firms
qui sum energymix_e, det 
g d_firm_gas = energymix_e > `r(p50)' if !mi(energymix_e)

*** Dummy for energy intensity 
qui sum share_cost_energy, det
g d_low_energy = share_cost_energy <= `r(p50)' if !mi(share_cost_energy)

*** Heterogeneity by increase in price
qui sum energypricechange if energypricechange > 0, det
g d_low_increase = inlist(energypricechange,0) | energypricechange < `r(p50)' if !mi(energypricechange) 
g d_high_increase = inlist(energypricechange,0) | energypricechange >= `r(p50)' if !mi(energypricechange)

*** Generate fixed effects
g d_monitor_aux = energy_a == 1 | energy_b == 1 
gegen d_monitor_energy = max(d_monitor_aux), by(BBk_Sample_Key)
g d_high_energy = 1 - d_low_energy
gegen d_low_energy_wave = group(d_low_energy wave)
g d_sector_nonint = 1 - d_sector_intensive
gegen d_sector_int_wave = group(d_sector_intensive wave)
g d_largefirm = 1 - d_smallfirm
gegen d_smallfirm_wave = group(d_smallfirm wave)
g d_non_firm_gas = 1 - d_firm_gas
gegen d_firm_gas_wave = group(d_firm_gas wave)
g d_no_monitor_energy = 1 - d_monitor_energy
gegen d_monitor_energy_wave = group(d_monitor_energy wave)
gegen firmregion_wave = group(firmregion_l wave) 
gegen sector_wave = group(firmsector_l wave)

*** Collapse data for Appendix Figure B.5
preserve 

gegen treated = max(tau), by(BBk_Sample_Key)

*** Calculate mean energy expenditure shares
foreach v of varlist energymix_f energymix_e energymix_a energymix_d {
	
	qui sum `v'
	local `v'_mean = `r(mean)'
	
}

local tot_mean = `energymix_f_mean' + `energymix_e_mean' + `energymix_a_mean' + `energymix_d_mean'
local share_elec = `energymix_f_mean' / `tot_mean'
local share_gas = `energymix_e_mean' / `tot_mean'
local share_oil = `energymix_a_mean' / `tot_mean'
local share_coal = `energymix_d_mean' / `tot_mean'

collapse (mean) inflexpoint, by(treated year month)

*** Merge market energy price data
merge m:1 year month using "${path_data}\marketprices.dta"
keep if _merge != 2
drop _merge

*** Generate aggregate energy index using the mean energy expenditure shares
g energy_price = `share_elec' *  (1 + price_elec) + `share_gas' * (1 + price_gas) + `share_oil' * (1 + price_oil) + `share_coal' * (1 + price_coal) 
qui sum energy_price if year == 21 & month == 7
replace energy_price = energy_price / `r(mean)' // Normalize price to July 2021

*** Appendix Figure B.5 
g time = ym(year+2000,month)
format %tmMon_CCYY time 

twoway (line inflexpoint time if treated == 1, yaxis(1) lcolor(blue)) || ///
	   (line inflexpoint time if treated == 0, yaxis(1) lcolor(red)) || ///
	   (line energy_price time, yaxis(2) lcolor(black) lpattern(dash)), ///
	   graphregion(color(white)) bgcolor(white) plotregion(fcolor(white)) xlabel(738(1)746, angle(90)) xtitle("") ytitle("energy price index rel. to Jul 2021", axis(2)) ytitle("inflation expectation (in pp)", axis(1)) legend(label(1 "treated firms") label(2 "control firms") label(3 "energy price index") order(1 2 3) rows(1) size(small) position(6)) ylabel(3(1)6, axis(1)) ylabel(1(0.5)2.5, axis(2))

graph export "${path_export_graphs}\appendix_figure_b5.pdf", replace 

restore 
 	
*** Appendix Table A.5: Part 2
qui estpost sum price_current inflexpoint pricefuture abs_predict_error sq_predict_error abs_difference_spf sq_difference_spf energymix_f energymix_e energymix_a energymix_d energymix_h share_cost_energy, det 
est store a

esttab a using "${path_export_tables}\appendix_table_a5b_part2.tex", replace nomtitles collabels(\multicolumn{1}{c}{mean} \multicolumn{1}{c}{median} \multicolumn{1}{c}{sd} \multicolumn{1}{c}{N}) cells("mean(fmt(2)) p50(fmt(2)) sd(fmt(2)) count(fmt(0))") coeflabels(price_current "realized price change (in pp)" inflexpoint "inflation expectation (in pp)" pricefuture "price expectation (in pp)" energymix_a "energy cost share: oil" energymix_d "energy cost share: coal" energymix_e "energy cost share: natural gas" energymix_f "energy cost share: electricity" energymix_h "energy cost share: other" share_cost_energy "share energy in total costs" abs_predict_error "abs. forecast error (in pp)" sq_predict_error "sq. forecast error (in pp)" abs_difference_spf "abs. difference to professional forecasters (in pp)" sq_difference_spf "sq. difference to professional forecasters (in pp)") nonumber f noobs alignment(S) booktabs postfoot("\bottomrule \bottomrule")

est clear 

*** Appendix Figure A.5b 
hist wave, frequency color(blue) graphregion(color(white)) bgcolor(white) plotregion(fcolor(white)) xlabel(6 "Jul 2021" 7 "Aug 2021" 8 "Sep 2021" 9 "Oct 2021" 10 "Nov 2021" 11 "Dec 2021" 12 "Jan 2022" 13 "Feb 2022" 14 "Mar 2022", angle(90)) xtitle("") ytitle("number of observations" " ") discrete gap(50)  

graph export "${path_export_graphs}\appendix_figure_a5b.pdf", replace 

preserve 

collapse (max) month_price_change, by(BBk_Sample_Key)

g quarter = 1 if month_price_change == 0 
replace quarter = 2 if inrange(month_price_change,6,8)
replace quarter = 3 if inrange(month_price_change,9,11)
replace quarter = 4 if inrange(month_price_change,12,14)

*** Figure 2a
hist quarter, percent xlabel(1 "no price change" 2 "2021 Q3" 3 "2021 Q4" 4 "2022 Q1", angle(90)) xtitle("") color(blue) discrete gap(50) graphregion(color(white)) bgcolor(white) plotregion(fcolor(white))

graph export "${path_export_graphs}\figure_2a.pdf", replace 

restore 

save "${path_data}\bopf_final.dta", replace

log close